/* eslint-disable react/no-unused-state */
/* eslint-disable react/sort-comp */
import { Component } from "react";
import { View } from "@tarojs/components";
import Taro from "@tarojs/taro";
import { AtList, AtListItem } from "taro-ui";
import "./news.scss";

export default class News extends Component {
  constructor() {
    super(...arguments);
    this.state = {
      newList: []
    };
  }

  componentWillMount() {
    this.getNewList();
  }

  componentDidMount() {}

  componentWillUnmount() {}

  componentDidShow() {}

  componentDidHide() {}

  render() {
    return (
      <View className="news">
        <AtList>
          {this.state.newList.map((res, i) => {
            return (
              <AtListItem
                onClick={this.ToDetail.bind(this, i)}
                key={i}
                title={res.title}
                arrow="right"
              />
            );
          })}
        </AtList>
      </View>
    );
  }
  getNewList() {
    let that = this;
    try {
      var newList = Taro.getStorageSync("newList");
      if (newList) {
        that.setState({
          newList: newList
        });
      } else {
        Taro.request({
          url: "https://dmyg.xyz:3000/hotNews",
          success: function(res) {
            console.log(res);
            that.setState({
              newList: res.data
            });
            console.log(that.state.newList);
            try {
              Taro.setStorage({
                key: "newList",
                data: res.data
              });
            } catch (e) {
              console.log(e);
            }
          }
        });
      }
    } catch (e) {
      console.log(e);
    }
  }
  // 跳转详情
  ToDetail(i) {
    console.log(i);
    Taro.navigateTo({
      url: `../newDetail/newDetail?index=${i}`
    });
  }
}
